package cn.sq.musicserver.plug.model.neteasecloudmusic;

import cn.sq.musicserver.exception.PlugError;
import cn.sq.musicserver.music.entity.DownloadParam;
import cn.sq.musicserver.music.entity.SqMusic;
import cn.sq.musicserver.plug.entity.PlugLyicResult;
import cn.sq.musicserver.plug.hander.MusicInfoSearc;
import cn.sq.musicserver.plug.model.jsososo.entity.LyricParam;
import cn.sq.musicserver.plug.model.neteasecloudmusic.entity.CloudSearchResult;
import cn.sq.musicserver.plug.model.neteasecloudmusic.entity.LoginParam;
import cn.sq.musicserver.plug.model.neteasecloudmusic.entity.SearchParam;
import cn.sq.musicserver.plug.model.neteasecloudmusic.service.NeteaseCloudMusicService;
import cn.sq.musicserver.plug.entity.PlugSearchResult;
import cn.sq.musicserver.set.entity.SqSet;
import io.github.classgraph.utils.Join;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: SQ
 * Date: 2020/12/11
 * Time: 13:44
 * Description:搜索音乐信息
 */
@Slf4j
@Component
public class NeteaseCloudMusicHander extends MusicInfoSearc {
    @Autowired
    NeteaseCloudMusicService neteaseCloudMusicService;

    @Override
    public String PlugName() {
        return "NeteaseCloudMusic";
    }

    @Override
    public String Plugversion() {
        return "1.0";
    }

    @Override
    public PlugSearchResult musicInfoSearc(Integer pagersize, Integer pageindex, String Keywords, File music, List<SqSet> set, Map<String, String> data) throws PlugError {
        log.debug(neteaseCloudMusicService.toString());
        //检测是否已经登录
        boolean islogin= false;
        try {
            islogin = neteaseCloudMusicService.neteaseCloudMusiCloginStatus();
        } catch (IOException e) {
            log.error("插件运行异常："+e.getMessage());
            throw new PlugError("登录失败");
        }
        if (islogin) {
            log.debug("已经成功");
        } else {
            log.debug("未登录开始登录");
            //未登录
            LoginParam loginParam = new LoginParam();
            int flag = 0;
            for (SqSet iteam : set) {
                if (flag == 2) {
                    break;
                }
                if (iteam.getSetName().equals("NeteaseCloudMusicEmail")) {
                    loginParam.setEmail(iteam.getSetVlaue());
                    flag++;
                    continue;
                }
                if (iteam.getSetName().equals("NeteaseCloudMusicPassword")) {
                    loginParam.setPassword(iteam.getSetVlaue());
                    flag++;
                    continue;
                }
            }
            //开始登录
            try {
                if (neteaseCloudMusicService.NeteaseCloudMusicLogin(loginParam)){
                    log.debug("登录成功");
                    islogin= true;
                }else{
                    log.error("登录失败");
                    throw  new PlugError("登录失败,请检查用户和密码是否正确");
                }
            } catch (IOException e) {
                log.error("插件运行异常："+e.getMessage());
                throw  new PlugError("登录失败,请检查用户和密码是否正确");
            }


        }
        if (islogin){
            SearchParam searchParam = new SearchParam().setKeywords(Keywords);
            searchParam.setOffset((pageindex-1)+"");
            searchParam.setLimit(pagersize+"");
            CloudSearchResult cloudSearchResult = null;
            try {
                cloudSearchResult = neteaseCloudMusicService.NeteaseCloudMusiCloudCloudsearch(searchParam);
            } catch (IOException e) {
                log.error("插件运行异常："+e.getMessage());
                throw new PlugError("插件运行io异常");
            }
            if(cloudSearchResult.getCode()!=200){
                throw  new PlugError(cloudSearchResult.getMessage());
            }else {
                List<CloudSearchResult.ResultBean.SongsBean> songs = cloudSearchResult.getResult().getSongs();
                ArrayList<String> strings = new ArrayList<>();
                PlugSearchResult plugSearchResult = new PlugSearchResult();
                List<PlugSearchResult.Song> ressongs = null;
                for (CloudSearchResult.ResultBean.SongsBean songsBean : songs) {
                    List<CloudSearchResult.ResultBean.SongsBean.ArBean> artists = songsBean.getAr();
                    for (CloudSearchResult.ResultBean.SongsBean.ArBean artist : artists) {
                        strings.add(artist.getName());
                    }
                    PlugSearchResult.Song song = new PlugSearchResult.Song()
                            .setMusicName(songsBean.getName())
                            .setMusicAlbum(songsBean.getAl().getName())
                            .setMusicArtists(Join.join(",", strings))
                            .setMusicImage(songsBean.getAl().getPicUrl());
                    ressongs.add(song);
                }
                plugSearchResult.setSongs(ressongs);
                plugSearchResult.setPlugMusicSearchCount(cloudSearchResult.getResult().getSongCount())
                        .setPlugMusicSearchIndex(new Long(pageindex))
                        .setPlugMusicSearchSize(new Long(pagersize));
                return plugSearchResult;
            }
        }else {
            return null;
        }
    }

    @Override
    public SqMusic musicDownload(DownloadParam downloadParam) throws PlugError {
        return null;
    }


    @Override
    public PlugLyicResult MusiclyricDownload(LyricParam lyricParam) throws PlugError {
        return null;
    }


}
